﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace winform_Vigenere
{
    internal class MyEncoder
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string PlainText { get; set; }
        /// <summary>
        /// 密文
        /// </summary>
        public string Cypher { get; set; }
        /// <summary>
        /// 密匙
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 加密动作
        /// </summary>
        public void Encode()
        {
            //明文
            Char[] nPlainText = PlainText.ToCharArray();
            //密匙
            Char[] nKey = Key.ToCharArray();
            //密文
            Char[] nCypher = new char[PlainText.Length];
            for (int i = 0; i < PlainText.Length; i++)
            {
                int n = (myFunction(nPlainText[i])+myFunction(nKey[i%Key.Length]))%26;
                nCypher[i]=Convert.ToChar(n+'a');
            }
            //将得到的字符数组转换为字符串赋值给Cypher
            Cypher=new string(nCypher);

        }
        /// <summary>
        /// 返回指定字符在字母表中序号，从0开始
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int myFunction(char ch)
        {
            return ch-'a';
        }

    }
}
